package com.hmdp.controller;

import com.hmdp.config.limit.Limit;
import com.hmdp.config.limit.LimitType;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @Description 测试限流的Controller
 * @Date 15:17 2023/2/1
 * @Author lizhihui
 * @ModifiedBy lizhihui
 */
@Api(tags = "测试限流")
@RestController
public class TestLimitController {

    private static final AtomicInteger ATOMIC_INTEGER_1 = new AtomicInteger();
    private static final AtomicInteger ATOMIC_INTEGER_2 = new AtomicInteger();
    private static final AtomicInteger ATOMIC_INTEGER_3 = new AtomicInteger();

    @ApiOperation("限流测试1-默认类型")
    @Limit(period = 10, count = 3)
    @GetMapping("/limitTest1")
    public int testLimiter1() {
        // key 为方法名
        return ATOMIC_INTEGER_1.incrementAndGet();
    }

    @ApiOperation("限流测试2-自定义")
    @Limit(key = "customer_limit_test", period = 10, count = 3, limitType = LimitType.CUSTOMER)
    @GetMapping("/limitTest2")
    public int testLimiter2() {
        // key 为用户指定的 key
        // 限制 10 秒内请求数为 3
        // 设置 Redis 的 TTL 及 value 值，服务内执行判断逻辑实现限流
        return ATOMIC_INTEGER_2.incrementAndGet();
    }

    @ApiOperation("限流测试3-IP")
    @Limit(period = 10, count = 3, limitType = LimitType.IP)
    @GetMapping("/limitTest3")
    public int testLimiter3() {
        // key 为服务解析出来的 IP
        return ATOMIC_INTEGER_3.incrementAndGet();
    }

}
